from flask import *
import jwt, time

# token
from jwt import ExpiredSignatureError


class JwtTool():
    def jwt_token(self, payload):
        payload['exp'] = int(time.time()) + 3600 * 24

        key = current_app.config.get('SECRET_KEY')
        token = jwt.encode(payload, key, algorithm='HS256')
        return token

    def jwt_long_token(self, payload):
        payload['exp'] = int(time.time()) + 3600 * 24 * 15

        key = current_app.config.get('SECRET_KEY')
        token = jwt.encode(payload, key, algorithm='HS256')
        return token

    def jwt_payload(self, token):
        key = current_app.config.get('SECRET_KEY')

        try:
            payload = jwt.decode(token, key, algorithms='HS256')
            return payload

        except ExpiredSignatureError as e:
            print(e)
            return 1

        except Exception as e:
            print(e, type(e))
            return False


if __name__ == '__main__':
    from app import app
    with app.app_context():
        s = JwtTool()
        token = s.jwt_token({'name': 'zhangsan'})
        print(JwtTool().jwt_payload(token))

